package com.kirichof.matchreminder.batchjob;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.kirichof.matchreminder.core.Log;

public class MatchReminderServletInitializationListener implements
		ServletContextListener {

	CalendarEventScannJob calendarScannerRunnable = new CalendarEventScannJob();
	Thread calendarScanner = new Thread(calendarScannerRunnable);

	public void contextDestroyed(ServletContextEvent arg0) {
		Log.getLogger().info("Shutting down calendarScanner thread...");
		calendarScannerRunnable.shutdown();
		Log.getLogger().info("Terminate signal has been sent to the thread");
		// try {
		// calendarScanner.join();
		// } catch (InterruptedException e) {
		// Log.getLogger()
		// .error("Error occured when joining the CalendarEventScannJob thread");
		// } finally {
		// Log.getLogger().info("CalendarScanner shut down succesfully");
		// Log.getLogger().info("Exiting servlet!");
		// }

	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		Log.getLogger().info("Context has been initialized! Hurray!");

		calendarScanner.start();
	}

}
